#!/bin/bash

echo "开始打包前端并部署到后端..."

# 获取前端目录
FRONTEND_DIR=$(pwd)
echo "前端目录: $FRONTEND_DIR"

# 计算后端目录
BACKEND_DIR="$FRONTEND_DIR/../jeecg-boot/jeecg-module-system/jeecg-system-start"
echo "后端目录: $BACKEND_DIR"

# 检查后端目录是否存在
if [ ! -d "$BACKEND_DIR" ]; then
    echo "错误: 后端目录不存在: $BACKEND_DIR"
    read -p "按任意键继续..."
    exit 1
fi

# 打包前端
echo ""
echo "正在打包前端..."
npm run build

if [ ! -d "dist" ]; then
    echo "错误: 打包失败，未生成dist目录"
    read -p "按任意键继续..."
    exit 1
fi

# 复制到后端静态资源目录
STATIC_DIR="$BACKEND_DIR/src/main/resources/static"
echo ""
echo "复制文件到后端静态资源目录: $STATIC_DIR"

# 创建或清空静态目录
if [ -d "$STATIC_DIR" ]; then
    rm -rf "$STATIC_DIR"
fi
mkdir -p "$STATIC_DIR"

# 复制文件
cp -r dist/* "$STATIC_DIR/"

echo ""
echo "部署完成! 现在可以启动后端服务访问前端应用了。"
read -p "按任意键继续..."